<?php

 class SimpleImage{
 
   var $image;
   var $image_type;
 
   function load($filename) {
 
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
 
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
 
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
 
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
 
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
 
         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {
 
         imagepng($this->image,$filename);
      }
      if( $permissions != null) {
 
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
 
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
 
         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {
 
         imagepng($this->image);
      }
   }
   function getWidth() {
 
      return imagesx($this->image);
   }
   function getHeight() {
 
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
 
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
 
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
 
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }
 
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   } 
   
   /**
    * Resize and fill background white
   */
   function resize_and_fill_background($source_image, $destination, $tn_w, $tn_h, $quality = 100, $wmsource = false)
   {
       $info = getimagesize($source_image);
       $imgtype = image_type_to_mime_type($info[2]);
   
       #assuming the mime type is correct
       switch ($imgtype) {
           case 'image/jpeg':
               $source = imagecreatefromjpeg($source_image);
               break;
           case 'image/gif':
               $source = imagecreatefromgif($source_image);
               break;
           case 'image/png':
               $source = imagecreatefrompng($source_image);
               break;
           default:
               die('Invalid image type.');
       }
   
       #Figure out the dimensions of the image and the dimensions of the desired thumbnail
       $src_w = imagesx($source);
       $src_h = imagesy($source);
   
   
       #Do some math to figure out which way we'll need to crop the image
       #to get it proportional to the new size, then crop or adjust as needed
   
       $x_ratio = $tn_w / $src_w;
       $y_ratio = $tn_h / $src_h;
   
       if (($src_w <= $tn_w) && ($src_h <= $tn_h)) {
           $new_w = $src_w;
           $new_h = $src_h;
       } elseif (($x_ratio * $src_h) < $tn_h) {
           $new_h = ceil($x_ratio * $src_h);
           $new_w = $tn_w;
       } else {
           $new_w = ceil($y_ratio * $src_w);
           $new_h = $tn_h;
       }
   
       $newpic = imagecreatetruecolor(round($new_w), round($new_h));
       imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
       $final = imagecreatetruecolor($tn_w, $tn_h);
       $backgroundColor = imagecolorallocate($final, 255, 255, 255);
       imagefill($final, 0, 0, $backgroundColor);
       //imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h);
       imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);
   
       #if we need to add a watermark
       if ($wmsource) {
           #find out what type of image the watermark is
           $info    = getimagesize($wmsource);
           $imgtype = image_type_to_mime_type($info[2]);
   
           #assuming the mime type is correct
           switch ($imgtype) {
               case 'image/jpeg':
                   $watermark = imagecreatefromjpeg($wmsource);
                   break;
               case 'image/gif':
                   $watermark = imagecreatefromgif($wmsource);
                   break;
               case 'image/png':
                   $watermark = imagecreatefrompng($wmsource);
                   break;
               default:
                   die('Invalid watermark type.');
           }
   
           #if we're adding a watermark, figure out the size of the watermark
           #and then place the watermark image on the bottom right of the image
           $wm_w = imagesx($watermark);
           $wm_h = imagesy($watermark);
           imagecopy($final, $watermark, $tn_w - $wm_w, $tn_h - $wm_h, 0, 0, $tn_w, $tn_h);
   
       }
       if (imagejpeg($final, $destination, $quality)) {
           return true;
       }
       return false;
   }     
 
}
?>